package org.nidy.erp.utils;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class MprFileCopier {
    public static void MPRFileCopier(String orderNumber, String batchNumber) throws IOException
    {
        // 1. 获取源文件地址
        String sourcePath = "\\\\172.16.70.4\\pxm\\" + orderNumber + "\\PTP160_QB";
        Path sourceDir = Paths.get(sourcePath);

        // 2. 从批次号中提取年月和月日
        String currentYear = batchNumber.substring(0, 2);
        String currentMonth= batchNumber.substring(2,4);
        String currentYearMonth ="20"+ currentYear+"-" + currentMonth;
        String currentMonthDay = batchNumber.substring(2, 6);

        String targetBasePath = "\\\\172.16.100.254\\e\\新系统开料排孔文件\\" + currentYearMonth + "\\" + currentMonthDay + "\\" +  batchNumber + "\\" + orderNumber;
        Path targetBaseDir = Paths.get(targetBasePath);
        Files.createDirectories(targetBaseDir);

        // 3. 将源文件目录下所有文件拷贝至目标文件地址
        Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path targetFile = targetBaseDir.resolve(sourceDir.relativize(file));
                Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                Path targetDir = targetBaseDir.resolve(sourceDir.relativize(dir));
                Files.createDirectories(targetDir);
                return FileVisitResult.CONTINUE;
            }
        });

        System.out.println("文件复制完成！");
    }
}
